package com.ferryc.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import com.ferryc.modules.sys.entity.SysOrganization;
import com.ferryc.modules.sys.entity.param.SysOrganizationParam;
import com.ferryc.modules.sys.service.ISysOrganizationService;

/**
 * 
 * 类名: SysOrganizationController <br/>
 * 作用： 控制<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年12月03日 07:33:41 <br/>
 */
@Controller
@RequestMapping("/organization")
public class SysOrganizationController extends BaseController {

	/** */
	@Autowired
	private ISysOrganizationService sysOrganizationService;

	/** */
	@RequiresPermissions("organization")
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/organization/list";
	}

	/**
	 * 树查询
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @return
	 */
	@GetMapping("/treeList")
	@ResponseBody
	public Object treeList(SysOrganizationParam sysOrganizationParam) {
		return sysOrganizationService.selectTreeList(sysOrganizationParam.toSysOrganization());
	}

	/**
	 * 列表查询
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @return
	 */
	@RequiresPermissions("organization")
	@PostMapping("/list")
	@ResponseBody
	public Object list(SysOrganizationParam sysOrganizationParam) {
		sysOrganizationParam.setStatus(StringUtil.EMPTY);
		SysOrganization sysOrganization = sysOrganizationParam.toSysOrganization();
		sysOrganization.setParentIdAndId(sysOrganizationParam.getParentId());
		sysOrganization.setParentId(StringUtil.EMPTY);
		return sysOrganizationService.selectPage(sysOrganization);
	}

	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@RequiresPermissions("organization_add")
	@GetMapping("/addPage")
	public Object addPage(SysOrganizationParam sysOrganizationParam, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "sys/organization/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("organization_add")
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid SysOrganizationParam sysOrganizationParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean result = sysOrganizationService.checkOrgName(sysOrganizationParam.toSysOrganization());
		if (!result) {
			return renderError(ResultConstant.EXISTS_ORG_NAME);
		}

		sysOrganizationParam.preInsert();
		sysOrganizationService.insert(sysOrganizationParam.toSysOrganization());
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("organization_view")
	@GetMapping("/viewPage")
	public Object viewPage(SysOrganizationParam sysOrganizationParam, Model model, HttpServletRequest request) {
		sysOrganizationParam.setStatus(StringUtil.EMPTY);
		SysOrganization sysOrganization = sysOrganizationService.get(sysOrganizationParam.getId());
		model.addAttribute("info", sysOrganization);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "sys/organization/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("organization_update")
	@GetMapping("/editPage")
	public Object editPage(SysOrganizationParam sysOrganizationParam, Model model, HttpServletRequest request) {
		sysOrganizationParam.setStatus(StringUtil.EMPTY);
		SysOrganization sysOrganization = sysOrganizationService.getByEntity(sysOrganizationParam.toSysOrganization());
		model.addAttribute("info", sysOrganization);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "sys/organization/edit";
	}

	/**
	 * 修改
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("organization_update")
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid SysOrganizationParam sysOrganizationParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean result = sysOrganizationService.checkOrgName(sysOrganizationParam.toSysOrganization());
		if (!result) {
			return renderError(ResultConstant.EXISTS_ORG_NAME);
		}
		sysOrganizationParam.preUpdate();
		sysOrganizationService.update(sysOrganizationParam.toSysOrganization());
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param sysOrganizationParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("organization_delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(SysOrganizationParam sysOrganizationParam, BindingResult bindingResult) {
		if (SysOrganization.SYSTEM_ID.equals(sysOrganizationParam.getId())) {
			return renderError(ResultConstant.ERROR_DEFAULT_SYSTEM);
		}
		SysOrganization sysOrganization = sysOrganizationParam.toSysOrganization();
		sysOrganization.setStatus(StringUtil.EMPTY);
		List<SysOrganization> list = sysOrganizationService.listByEntity(sysOrganization);
		if (!CollectionUtils.isEmpty(list)) {
			return renderError(ResultConstant.EXISTS_ORG_CHILD);
		}
		sysOrganizationParam.preUpdate();
		sysOrganizationParam.setDelFlag(GlobalConstant.YES);
		sysOrganizationService.update(sysOrganizationParam.toSysOrganization());
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}
}